<template>
  <div class="l-rblock l-form-viewer erpCase" style="padding: 8px">
    <el-form
      :model="formData"
      :rules="rules"
      size="mini"
      labelPosition="right"
      labelWidth="80px"
      ref="form"
    >
      <div class="l-auto-window only-tabs">
        <el-tabs v-model="activeName" type="card">
          <el-tab-pane
            class="l-tabs-container"
            :label="$t('主表信息')"
            name="tab0"
          >
            <el-row :gutter="16">
              <div class="l-rblock">
                <div class="form_name right_right">{{ $t("基本信息") }}</div>
                <div class="form_information form_back form_top_upload">
                  <div class="font_size_14">
                    {{ $t("客户名称：" + formData.case_erp_customer_f_Name) }}
                  </div>
                  <div class="form_left_32">
                    {{ $t("客户类型：" + formData.case_erp_customer_f_Type) }}
                  </div>
                  <div class="form_left_32">
                    {{
                      $t("所在行业：" + formData.case_erp_customer_f_Industry)
                    }}
                  </div>
                  <div class="form_left_32">
                    {{ $t("来源：" + formData.case_erp_customer_f_Source) }}
                  </div>
                </div>
                <el-col :span="24">
                  <div class="form_name form_top_upload">
                    {{ $t("客户跟进") }}
                  </div>
                  <div class="form_top_upload">
                    <el-col :span="12">
                      <el-form-item
                        :label="$t('跟进方式')"
                        prop="case_erp_customerfollow_f_Type"
                      >
                        <l-select
                          :options="case_erp_customerfollow_f_TypeOptions"
                          v-model="formData.case_erp_customerfollow_f_Type"
                        />
                      </el-form-item>
                    </el-col>
                    <el-col :span="12">
                      <el-form-item
                        :label="$t('跟进时间')"
                        prop="case_erp_customerfollow_f_VisitTime"
                      >
                        <l-date
                          :readonly="false"
                          format="yyyy-MM-dd"
                          dateType="date"
                          :clearable="true"
                          v-model="formData.case_erp_customerfollow_f_VisitTime"
                          :placeholder="$t('please select')"
                        />
                      </el-form-item>
                    </el-col>
                    <el-col :span="24">
                      <el-form-item
                        :label="$t('下次跟进')"
                        prop="case_erp_customerfollow_f_NextVisitTime"
                      >
                        <l-date
                          :readonly="false"
                          format="yyyy-MM-dd"
                          dateType="date"
                          :clearable="true"
                          v-model="
                            formData.case_erp_customerfollow_f_NextVisitTime
                          "
                          :placeholder="$t('please select')"
                        />
                      </el-form-item>
                    </el-col>
                    <el-col :span="24">
                      <el-form-item
                        :label="$t('说明')"
                        prop="case_erp_customerfollow_f_Content"
                      >
                        <el-input
                          v-model="formData.case_erp_customerfollow_f_Content"
                          :placeholder="$t('please enter')"
                        >
                        </el-input>
                      </el-form-item>
                    </el-col>
                    <el-col :span="24">
                      <el-form-item
                        :label="$t('图片上传')"
                        prop="case_erp_customerfollow_f_File"
                      >
                        <l-upload
                          maxSize="2"
                          sizeType="MB"
                          :limit="9"
                          v-model="formData.case_erp_customerfollow_f_File"
                          listType="picture-card"
                          :placeholder="$t('please enter')"
                        />
                      </el-form-item>
                    </el-col>
                  </div>
                </el-col>
              </div>
            </el-row>
          </el-tab-pane>
        </el-tabs>
      </div>
    </el-form>
  </div>
</template>
<script>
const api = window.$api.erpcase.customer;

export default {
  data() {
    return {
      moduleCode: "ERP_Customers",
      activeName: "tab0",
      formData: {
        case_erp_customer_f_Name: "", // 客户名称
        case_erp_customer_f_Type: "", // 客户类型
        case_erp_customer_f_Industry: "", // 所在行业
        case_erp_customer_f_Source: "", // 来源
        case_erp_customerfollow_f_Type: "", // 跟进方式
        case_erp_customerfollow_f_VisitTime: "", // 跟进时间
        case_erp_customerfollow_f_NextVisitTime: "", // 下次跟进
        case_erp_customerfollow_f_Content: "", // 说明
        case_erp_customerfollow_f_File: "", // 图片上传
      },
      rules: {
        case_erp_customerfollow_f_Type: [
          { required: true, message: "please select跟进方式" },
        ],
        case_erp_customerfollow_f_VisitTime: [
          { required: true, message: "please select跟进时间" },
        ],
      },
    };
  },
  computed: {
    case_erp_customerfollow_f_TypeOptions() {
      this.lr_loadDataItem("FollowType");
      return this.lr_dataItemOptions(this.lr_dataItem["FollowType"]);
    },
  },
  methods: {
    // Reset表单
    resetForm() {
      this.$formClear(this.$refs.form);
      this.activeName = "tab0";
    },
    // 校验表单
    async validateForm() {
      if (!(await this.$formValidateWraper(this.$refs.form))) {
        return false;
      }

      return true;
    },
    // 设置表单数据
    setForm(data) {
      // console.log('跟进数组', data)
      this.$setFormData(
        this.formData,
        data.caseErpCustomerEntity,
        "case_erp_customer"
      );
      this.$setFormData(
        this.formData,
        data.caseErpCustomerfollowEntity,
        "case_erp_customerfollow"
      );
    },
    // 获取表单数据
    getForm() {
      const formData = {};
      formData.caseErpCustomerEntity = this.$getFormData(
        this.formData,
        "case_erp_customer"
      );
      formData.caseErpCustomerfollowEntity = this.$getFormData(
        this.formData,
        "case_erp_customerfollow"
      );
      return formData;
    },

    async loadFormData({ keyValue /*,params,node*/, customerId }) {
      //  keyValue 流程中相当于流程processId,params 传递参数,node 流程节点
      const data = await this.$awaitWraper(api.get(keyValue));

      if (customerId != null) {
        data.caseErpCustomerfollowEntity = await this.$awaitWraper(
          api.oneCustomerfollow(customerId)
        );
        // console.log('加载的数据', data.caseErpCustomerfollowEntity)
      }
      if (data) {
        this.setForm(data);
        return true;
      } else {
        return false;
      }
    },

    async saveForm({ isEdit }) {
      // isEdit 是否更新数据, keyValue 流程中相当于流程processId,code 表示流程中的操作码,node 流程节点
      const postData = this.getForm();
      console.log("Edit", this.formData);
      if (isEdit) {
        // Edit

        // console.log('跟进Edit', keyValue, isEdit)
        return await this.$awaitWraper(
          api.exitCustomerfollow(
            this.formData.case_erp_customerfollow_f_Id,
            postData.caseErpCustomerfollowEntity
          )
        );
      } else {
        // Add
        // console.log('跟进Add', )
        postData.caseErpCustomerfollowEntity.f_CustomerId =
          this.formData.case_erp_customer_f_Id;

        return await this.$awaitWraper(
          api.customerfollow(postData.caseErpCustomerfollowEntity)
        );
      }
    },
    resetFormData() {
      this.formData = {
        case_erp_customer_f_Name: "", // 客户名称
        case_erp_customer_f_Type: "", // 客户类型
        case_erp_customer_f_Industry: "", // 所在行业
        case_erp_customer_f_Source: "", // 来源
        case_erp_customerfollow_f_Type: "", // 跟进方式
        case_erp_customerfollow_f_VisitTime: "", // 跟进时间
        case_erp_customerfollow_f_NextVisitTime: "", // 下次跟进
        case_erp_customerfollow_f_Content: "", // 说明
        case_erp_customerfollow_f_File: "", // 图片上传
      }
    }
  },
};
</script>